<template>
	<n-modal
		v-model:show="showModal"
		title="驳回原因"
		preset="dialog"
		:show-icon="false"
		:mask-closable="false"
		@close="handleClose"
	>
		<div>
			<n-form :model="formValue" label-placement="left" :show-feedback="false">
				<n-form-item>
					<n-input
						v-model:value="formValue.backMessage"
						type="textarea"
						placeholder="请输入"
						maxlength="100"
						minlength="1"
						show-count
					/>
				</n-form-item>
			</n-form>
		</div>
		<template #action>
			<n-button size="small" color="rgba(0, 0, 0, 0.1)" text-color="#333333" @click="handleClose">
				关闭
			</n-button>
			<n-button
				:disabled="disabledBtn"
				type="primary"
				color="#F55F4E"
				text-color="#FFFFFF"
				size="small"
				@click="approve"
			>
				确认驳回
			</n-button>
		</template>
	</n-modal>
</template>

<script setup>
import {
	applyDetail,
	auditWithPerson,
	auditWithPosition,
	auditReviewCheck
} from '@/api/applyReview';
import { ref, reactive, watch } from 'vue';
import { useMessage } from 'naive-ui';
import { useRouter } from 'vue-router';
import dayjs from 'dayjs';

const message = useMessage();
const router = useRouter();

const props = defineProps({
	modal: Boolean,
	applyId: {
		typeof: Number,
		default: 0
	}
});

const emit = defineEmits(['update:modal', 'commit']);

const showModal = ref(props.modal);
const disabledBtn = ref(false);

watch(
	() => ({ ...props }),
	(count, prevCount) => {
		showModal.value = count.modal;
		if (count.modal) {
			formValue.backMessage = null;
			getApplyDetail();
		}
	}
);

// 表单数据
const formValue = reactive({ backMessage: null });
// 操作关闭
const handleClose = (e) => {
	showModal.value = false;
	emit('update:modal', showModal.value);
};

//获取审核详情
let detailInfo = ref({ auditType: null, finalExploreTime: null, finalPrice: null, id: null });
const getApplyDetail = async () => {
	disabledBtn.value = false;
	const setForm = {
		id: props.applyId
	};
	console.log(1000, setForm);
	const res = await applyDetail(setForm);
	if (res.code === 200) {
		detailInfo.value = res.data;
	}
};

// 审核
const approve = async () => {
	if (!formValue.backMessage) {
		message.error('驳回原因不能为空');
		return;
	}
	const params = {
		auditType: detailInfo.value.auditType,
		finalExploreTime:
			detailInfo.value.finalExploreTime == null
				? null
				: dayjs(detailInfo.value.finalExploreTime).format('YYYY-MM-DD HH:mm') + ':00',
		finalPrice: detailInfo.value.finalPrice,
		message: formValue.backMessage,
		result: 2,
		seekId: detailInfo.value.id,
		videoKey: null
	};
	const fullPath = router.currentRoute.value.fullPath;
	let res = null;
	if (
		fullPath === '/apply-review/findListWithPerson' ||
		fullPath === '/apply-review/findListWithPersonAdmin'
	) {
		disabledBtn.value = true;
		res = await auditWithPerson(params);
	} else if (
		fullPath === '/apply-review/findListWithPosition' ||
		fullPath === '/apply-review/findListWithPositionAdmin'
	) {
		disabledBtn.value = true;
		res = await auditWithPosition(params);
	} else if (
		fullPath === '/apply-review/findListReviewCheck' ||
		fullPath === '/apply-review/findListReviewCheckAdmin'
	) {
		disabledBtn.value = true;
		res = await auditReviewCheck(params);
	} else {
		message.error('非法操作！');
		return;
	}
	if (res.code === 200) {
		message.success('操作成功');
		formValue.backMessage = null;
		showModal.value = false;
		emit('update:modal', showModal.value);
		emit('commit');
	} else {
		message.error(res.message);
		disabledBtn.value = false;
	}
};
</script>
